﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;

namespace ElectronicCommerce.Framework.Payment.Utility
{
    /// <summary>
    /// 
    /// </summary>
    public static class FormHelper
    {
        private static readonly string HTML_HEAD = "<html><head><title>正在与支付网关通信,请不要关闭或刷新浏览器...</title></head><body style='text-align:center;'>";
        private static readonly string HTML_HEAD_WITH_ENCODING = "<html><head><title>正在与支付网关通信,请不要关闭或刷新浏览器...</title><meta name=\"Content-Type\" content=\"text/html; charset={0}\" /></head><body style='text-align:center;'>";

        private static readonly string HTML_CONTENT = "<div style='font-family:宋体 Arial Verdana;margin-top:80px;background-color:#FFFFEE;background-position:20px 20px;background-repeat:no-repeat;border:1px solid #FFF0A8;font-size:14px;height:120px;min-height:100px;padding:20px 20px 30px;width:80%;font-weight:bold;'><h1>正在与支付网关通信,请不要关闭或刷新浏览器.....</h1></div>";

        private static readonly string HTML_FOOTER = "</body></html>";

        private static readonly string HTML_FORMAT_FORM = "<form id=\"payform\" name=\"payform\" action=\"{0}\" method=\"POST\">{1}</form>";
        private static readonly string HTML_FORMAT_INPUT = ("<input type=\"hidden\" id=\"{0}\" name=\"{0}\" value=\"{1}\">" + Environment.NewLine);
        private static readonly string HTML_SUBMIT_SCRIPT = "<script type=\"text/javascript\" language=\"javascript\">window.onload = function(){document.getElementById('payform').submit();}</script>";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string CreateHiddenField(string name, string value)
        {
            return string.Format(CultureInfo.InvariantCulture, HTML_FORMAT_INPUT, new object[] { name, value });
        }

        public static string CreateForm(string content, string action)
        {
            return string.Format(CultureInfo.InvariantCulture, HTML_FORMAT_FORM, new object[] { action, content });
        }

        public static void SubmitForm(string formContent)
        {
            string text = HTML_HEAD + HTML_CONTENT + formContent + HTML_SUBMIT_SCRIPT + HTML_FOOTER;
            HttpContext.Current.Response.Write(text);
            HttpContext.Current.Response.End();
        }
        public static void SubmitForm(string encoding, string formContent)
        {
            string text = string.Format(HTML_HEAD_WITH_ENCODING, encoding) + HTML_CONTENT + formContent + HTML_SUBMIT_SCRIPT + HTML_FOOTER;
            HttpContext.Current.Response.Write(text);
            HttpContext.Current.Response.End();
        }
    }
}
